package com.example.my_objectives.model;

import java.util.ArrayList;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class Objective extends Database {

	private final String	LOG	= Objective.class.getName();

	private long			_id;
	private String			_name;
	private String			_description;
	private double			_value;
	private Unit			_unit;
	private Recurrence	_recurrence;
	private boolean		_is_notifiable;
	private String			_date;
	private String			_time;

	public Objective(SQLiteDatabase db) {
		super(db);
		set_database(db);
		this._recurrence = new Recurrence(get_database());
	}

	public Objective(SQLiteDatabase db, long id, String name, String description, double value, long id_unit,
			long id_recurrence, boolean is_notifiable, String date, String time) {
		super(db);
		set_database(db);
		this._id = id;
		this._name = name;
		this._description = description;
		this._value = value;
		this._recurrence = new Recurrence(get_database());
		this._recurrence.load(id_recurrence);
		this._is_notifiable = is_notifiable;
		this._date = date;
		this._time = time;
	}

	public long getId() {
		return _id;
	}

	public void setId(long id) {
		this._id = id;
	}

	public String getName() {
		return _name;
	}

	public void setName(String name) {
		this._name = name;
	}

	public String getDescription() {
		return _description;
	}

	public void setDescription(String description) {
		this._description = description;
	}

	public double getValue() {
		return _value;
	}

	public void setValue(double value) {
		this._value = value;
	}

	public Unit getUnit() {
		return _unit;
	}

	public void setUnit(Unit unit) {
		this._unit = unit;
	}

	public boolean is_notifiable() {
		return _is_notifiable;
	}

	public void set_notifiable(boolean is_notifiable) {
		this._is_notifiable = is_notifiable;
	}

	public Recurrence getRecurrence() {
		return _recurrence;
	}

	public void setRecurrence(Recurrence recurrence) {
		this._recurrence = recurrence;
	}

	public String getDate() {
		return _date;
	}

	public void setDate(String date) {
		this._date = date;
	}

	public String getTime() {
		return _time;
	}

	public void setTime(String time) {
		this._time = time;
	}

	@Override
	public Exception createTable() {
		Exception returnValue = null;
		try {
			String query = "CREATE TABLE " + TableName.objective.toString()
				+ "(id INTEGER, name TEXT, description TEXT, value REAL, id_unit INTEGER, "
				+ "id_recurrence INTEGER, is_notifiable INTEGER, date TEXT, time TEXT, "
				+ "CONSTRAINT 'objective_pk' PRIMARY KEY(id), "
				+ "CONSTRAINT 'objective_fk_unit' FOREIGN KEY(id_unit) REFERENCES " + TableName.unit.toString() + "(id), "
				+ "CONSTRAINT 'objective_fk_recurrence' FOREIGN KEY(id_recurrence) REFERENCES "
				+ TableName.recurrence.toString() + "(id))";
			Log.d(LOG, query);
			get_database().execSQL(query);
		}// try
		catch (Exception e) {
			returnValue = e;
			Log.e(LOG, e.getMessage());
		}// catch
		return returnValue;
	}

	@Override
	public Exception add() {
		Exception eReturnValue = null;
		try {
			ContentValues values = new ContentValues();
			if (_id != -1) {
				values.put("id", _id);
			}// if
			values.put("name", _name);
			values.put("description", _description);
			values.put("value", _value);
			values.put("id_unit", _unit.getId());
			values.put("id_recurrence", _recurrence.getId());
			values.put("is_notifiable", _is_notifiable);
			values.put("date", _date);
			values.put("time", _time);

			get_database().insert(TableName.objective.toString(), null, values);
		}// try
		catch (Exception e) {
			eReturnValue = e;
			Log.e(LOG, e.getMessage());
		}// catch
		return eReturnValue;
	}

	@Override
	public Exception update(long old_id) {
		Exception eReturnValue = null;
		try {
			ContentValues values = new ContentValues();
			values.put("id", _id);
			values.put("name", _name);
			values.put("description", _description);
			values.put("value", _value);
			values.put("id_unit", _unit.getId());
			values.put("id_recurrence", _recurrence.getId());
			values.put("is_notifiable", _is_notifiable);
			values.put("date", _date);
			values.put("time", _time);

			get_database()
					.update(TableName.objective.toString(), values, "id = ?", new String[] { Long.toString(old_id) });
		}// try
		catch (Exception e) {
			eReturnValue = e;
			Log.e(LOG, e.getMessage());
		}// catch
		return eReturnValue;
	}

	@Override
	public Exception delete() {
		Exception eReturnValue = null;
		try {
			String query = "DELETE FROM " + TableName.objective.toString() + " WHERE id = " + _id;
			Log.d(LOG, query);
			get_database().execSQL(query);
		}// try
		catch (Exception e) {
			eReturnValue = e;
			Log.e(LOG, e.getMessage());
		}// catch
		return eReturnValue;
	}

	@Override
	public Exception load(long id) {
		Exception returnValue = null;
		try {
			String query = "SELECT * FROM " + TableName.objective.toString() + " WHERE id = " + id;
			Log.d(LOG, query);
			Cursor cursor = get_database().rawQuery(query, null);

			if (cursor.moveToFirst()) {
				_id = id;
				_name = cursor.getString(cursor.getColumnIndex("name"));
				_description = cursor.getString(cursor.getColumnIndex("description"));
				_value = cursor.getDouble(cursor.getColumnIndex("value"));
				_unit = new Unit(get_database());
				_unit.load(cursor.getLong(cursor.getColumnIndex("id_unit")));
				_recurrence = new Recurrence(get_database());
				_recurrence.load(cursor.getLong(cursor.getColumnIndex("id_recurrence")));
				_is_notifiable = cursor.getInt(cursor.getColumnIndex("is_notifiable")) == 0 ? false : true;
				_date = cursor.getString(cursor.getColumnIndex("date"));
				_time = cursor.getString(cursor.getColumnIndex("time"));
			}// if
			cursor.close();
		}// try
		catch (Exception e) {
			returnValue = e;
			Log.e(LOG, e.getMessage());
		}// catch
		return returnValue;
	}

	/**
	 * All objectives
	 *
	 * @return All records in the table
	 */
	public ArrayList<ContentValues> getAll() {

		ArrayList<ContentValues> list = new ArrayList<ContentValues>();

		try {
			String query = "SELECT * FROM " + TableName.objective.toString();
			Log.d(LOG, query);
			Cursor cursor = get_database().rawQuery(query, null);
			if (cursor.moveToFirst()) {
				while (!cursor.isAfterLast()) {
					ContentValues record = new ContentValues();
					record.put("id", cursor.getString(cursor.getColumnIndex("id")));
					record.put("name", cursor.getString(cursor.getColumnIndex("name")));
					record.put("description", cursor.getString(cursor.getColumnIndex("description")));
					record.put("value", cursor.getString(cursor.getColumnIndex("value")));
					record.put("id_unit", cursor.getString(cursor.getColumnIndex("id_unit")));
					record.put("id_recurrence", cursor.getString(cursor.getColumnIndex("id_recurrence")));
					record.put("is_notifiable", cursor.getString(cursor.getColumnIndex("is_notifiable")));
					record.put("date", cursor.getString(cursor.getColumnIndex("date")));
					record.put("time", cursor.getString(cursor.getColumnIndex("time")));

					list.add(record);
				}// while
			}// if
			cursor.close();
		}//try
		catch (Exception e) {
			Log.e(LOG, e.getMessage());
		}//catch
		return list;
	}

}
